<?php 
X_mysql::$cacheFile = APPLICATION_ROOT.'/cache/database_cache.sql';
X_mysql::$schemaFile = APPLICATION_ROOT.'/cache/database_schema.sql';
X_mysql::$RebuildTables      =  !is_file(X_mysql::$cacheFile); // Delete Cache file when editing a table's structure to alter automatically. This will NOT delete any data.
X_mysql::$DropBeforeRebuild  =  Config::isDEV() && !is_file(X_mysql::$schemaFile); // Delete Schema file to rebuild the entire database, erasing all data. (DEV mode only).

function configureTables(X_mysql $database) {
    
    if ( X_mysql::$RebuildTables ) {
        if (X_mysql::$DropBeforeRebuild) {
            $database->query("DROP DATABASE `".X_mysql::$Login['database']."`");
            $database->query("CREATE DATABASE `".X_mysql::$Login['database']."`");
            $database->query("USE `".X_mysql::$Login['database']."`");
            
            X_mysql::deleteSchemaFile();
            X_mysql::storeInSchemaFile("-- Schema for Database '".X_mysql::$Login['database']."' Created on ".date('Y-m-d H:i:s')."\n");
        } else {
            X_mysql::storeInSchemaFile("\n\n-- Structure Updated On ".date('Y-m-d H:i:s')." --------------- \n\n");
        }
    }
    
    $database->configureTable('user', array(
        'u_id'        => 'INT(11) AUTO_INCREMENT', 
        'u_username'  => 'VARCHAR(255), INDEX(u_username)', 
        'u_password'  => 'VARCHAR(255)', 
        'u_name'      => 'VARCHAR(255)', 
        'u_email'     => 'VARCHAR(255), INDEX(u_email)', 
        'u_created'   => 'TIMESTAMP DEFAULT CURRENT_TIMESTAMP', 
        'u_active'    => 'TINYINT(1) DEFAULT 1', 
        'u_deleted'   => 'TINYINT(1) DEFAULT 0', 
        
    ), 'u_id', 'ENGINE=InnoDB CHARACTER SET utf8 COLLATE utf8_general_ci');
    
    $database->configureTable('i18n', array(
        'ID' => 'VARCHAR(255)', 
        'en' => 'VARCHAR(255)', 
        'fr' => 'VARCHAR(255)', 
        'es' => 'VARCHAR(255)', 
    ), 'ID', 'ENGINE=Myisam CHARACTER SET utf8 COLLATE utf8_general_ci');
    
    
    if (Config::isDEV()) { // Queries below this line will only execute in DEV mode
        
        if ( X_mysql::$RebuildTables ) {
            
            if ( X_mysql::$DropBeforeRebuild ) { // Initial Data to insert Once

                $database->insert('user', array(
                    'u_id' => 1, 
                    'u_name' => 'Admin',
                    'u_username' => 'admin',
                    'u_password' => md5('12345'), // Please Change This Password
                    'u_active' => true,
                ));

            }
            
            // Initial Data that will be updated each time the cache file is deleted
            // ...

            X_mysql::storeInSchemaFile(X_mysql_query::getDebugQueriesAndClear()); // Store above queries to schema file as part of the initial data structure
        }
        
        X_mysql_query::$debugFile = APPLICATION_ROOT.'/cache/database_debug.sql'; // Set Debug File, set null or comment this line if you do not want a debug file in DEV mode
        X_mysql_query::emptyDebugFile(); // Reset Debug File once per script execution, comment this line to keep all the previous Debug Queries
    }
}